local skel = fk.CreateSkill({
  name = "lb__hanying",
  max_branches_use_time = {
    ["damage"] = {
      [Player.HistoryRound] = 1
    },
    ["damaged"] = {
      [Player.HistoryRound] = 1
    },
  },
})

Fk:loadTranslationTable {
  ["lb__hanying"] = "寒影",
  [":lb__hanying"] = "每轮各限一次，当你造成或受到伤害时，你可以将之改为冰冻伤害。当你受到冰冻伤害时，你可以弃置两张牌以防止之。",

  ["#lb__hanying"] = "寒影：你可以弃置两张牌以防止你即将受到的%arg点冰冻伤害",
  ["#lb__hanying_1"] = "寒影：你可以将你即将造成的%arg点%arg2伤害改为冰冻伤害",
  ["#lb__hanying_2"] = "寒影：你可以将你即将受到的%arg点%arg2伤害改为冰冻伤害",

  ["$lb__hanying1"] = "感受冰寒。",
  ["$lb__hanying2"] = "感受雪崩！",
  ["$lb__hanying3"] = "存护的力量。",
  ["$lb__hanying4"] = "就此掩埋。",
}

local hanying_spec = {
  audio_index = {1, 2},
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and data.damageType ~= fk.IceDamage and not data.prevented
    and skel:withinBranchTimesLimit(player, event == fk.DamageCaused and "damage" or "damaged", Player.HistoryRound)
  end,
  on_cost = function(self, event, target, player, data)
    local prompt = event == fk.DamageCaused and "#lb__hanying_1" or "#lb__hanying_2"
    return player.room:askToSkillInvoke(player,
      { skill_name = skel.name,
        prompt = prompt .. ":::" .. data.damage .. ":" .. Fk:getDamageNatureName(data.damageType)
      })
  end,
  on_use = function(self, event, target, player, data)
    player:addSkillBranchUseHistory(skel.name, event == fk.DamageCaused and "damage" or "damaged")
    data.damageType = fk.IceDamage
  end,
}

skel:addEffect(fk.DamageCaused, hanying_spec, { check_skill_limit = true })
skel:addEffect(fk.DamageInflicted, hanying_spec, { check_skill_limit = true })

skel:addEffect(fk.DetermineDamageInflicted, {
  audio_index = {3, 4},
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and #player:getCardIds("he") > 1
    and data.damageType == fk.IceDamage and not data.prevented
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local cards = room:askToDiscard(player, {
      max_num = 2,
      min_num = 2,
      skill_name = "lb__hanying",
      cancelable = true,
      prompt = "#lb__hanying:::" .. data.damage,
      include_equip = true,
      skip = true,
    })
    if #cards > 1 then
      event:setCostData(self, { cards = cards })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local cards = event:getCostData(self).cards
    player.room:throwCard(cards, skel.name, player)
    data:preventDamage()
  end,
})

return skel